<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .odiv {
            /* white-space: normal;前面空格 */
            /* white-space: nowrap; 不换行 */
            /* white-space: pre; */
            /* white-space: pre-wrap; */
        }
    </style>
</head>

<body>
    <div class="odiv"> 我是 hello world 你是谁 </div>
    <script>
        // let str='  123 abc   '
        // // 功能: 1)去除字符串前后所有空格 (trim()前后空格)
        // // 2)去除字符串中所有空格(包括中间空格,需要设置第2个参数为:g) 
        // function Trim(str, is_global='i') {
        //     var result;
        //     result = str.replace(/(^\s+)|(\s+$)/g, "");
        //     if (is_global.toLowerCase() == "g")
        //         result = result.replace(/\s/g, "");
        //     return result;
        // } 
        // console.log(Trim(str,'g'),'----------')

        String.prototype.ctrim = function (is_global = 'i') {
            var result;
            result = this.replace(/(^\s+)|(\s+$)/g, "");
            if (is_global.toLowerCase() == "g")
                result = result.replace(/\s/g, "");
            return result;
        }

         let str='  123 abc   '

         console.log(str.ctrim('g'),'=========')
    </script>
</body>

</html>